#!/bin/bash
##############################################################
# Usage:
# ./execute_test <type> <file/class> <timeout> <path>
##############################################################


SANDBOX=$2
TESTFILE=$3
TEST_TYPE=$1
TIMEOUT=$4
TEST_PATH=$5
TEST_JUDGE_TYPE=$6
MY_PATH=$( cd "$( dirname "$0" )" && pwd )

if [[ "$SANDBOX" == "implicit" ]] ; then
	if [[ "$TEST_TYPE" == "jUnit" ]] ; then
		$MY_PATH/prepare_jUnit_test $TESTFILE $TIMEOUT $TEST_PATH
		exit $?
	elif [[ "$TEST_TYPE" == "pyUnit" ]] ; then
		$MY_PATH/prepare_pyUnit_test $TESTFILE $TIMEOUT $TEST_PATH
		exit $?
	elif [[ "$TEST_TYPE" == "pythonIO" ]] ; then
		$MY_PATH/prepare_pythonIO_test $TESTFILE $TIMEOUT $TEST_PATH $TEST_JUDGE_TYPE
		exit $?
	fi
elif [[ "$SANDBOX" == "docker" ]] ; then
	docker run --rm=true list/sandbox echo "" > /dev/null 2> /dev/null
	dockerrunning=$?
	if [[ $dockerrunning > 0 ]]; then
		echo "Docker servis is not running! Switching task to implicit sandbox."
		$MY_PATH/execute_test $1 implicit $3 $4 $5 $6
		exit $?
	fi
	if [[ "$TEST_TYPE" == "jUnit" ]] ; then
		docker run --rm=true --net="none" --volume="$TEST_PATH:/list_test_files" list/sandbox run_test jUnit $TESTFILE $TIMEOUT
		exit $?
	elif [[ "$TEST_TYPE" == "pyUnit" ]] ; then
		docker run --rm=true --net="none" --volume="$TEST_PATH:/list_test_files" list/sandbox run_test pyUnit $TESTFILE $TIMEOUT
		exit $?
	elif [[ "$TEST_TYPE" == "pythonIO" ]] ; then
		docker run --rm=true --net="none" --volume="$TEST_PATH:/list_test_files" list/sandbox run_test pyIO $TESTFILE $TIMEOUT $TEST_JUDGE_TYPE
		exit $?
	fi
fi

exit 150
